package handle

import (
	"fileServer/cfile"
	"fileServer/global"
	"github.com/gin-gonic/gin"
	"log"
	"os"
)

func GetFilePath(c *gin.Context) (filePath string, err error) {
	path, err := ParsePath(c)
	if err != nil {
		return
	}
	filePath, err = cfile.SplitUrl(path.Path)
	if err != nil {
		global.GetResult(c).Error(400401, "路径错误", err.Error())
		return
	}
	return
}

func DeleteFile(c *gin.Context) {
	filePath, _ := GetFilePath(c)
	fileInfo, err := os.Stat(filePath)
	if err != nil {
		global.GetResult(c).Error(400101, "获取文件信息失败", err.Error())
		return
	}
	if fileInfo.IsDir() {
		log.Println("remove for dir:", filePath)
		err := os.RemoveAll(filePath)
		if err != nil {
			log.Panic(err)
			return
		}
	} else {
		log.Println("remove path::", filePath)
		err := os.Remove(filePath)
		if err != nil {
			log.Panic(err)
			return
		}
	}
	global.GetResult(c).Success("删除文件成功")
}
